package com.foredu.fiqs.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.SalaryItemDetailType;
import com.foredu.fiqs.domain.SalaryItemType;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.SalaryItemDetailTypeActionForm;

public class SalaryItemDetailTypeAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<SalaryItemType> types = managementService.findSalaryItemType();
		request.setAttribute("types", types);
		List<SalaryItemDetailType> detailTypes = managementService
				.findSalaryItemDetailType();
		request.setAttribute("detailTypes", detailTypes);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemDetailTypeActionForm saf = (SalaryItemDetailTypeActionForm) form;
		SalaryItemDetailType detailType = new SalaryItemDetailType();
		detailType.setName(saf.getName());
		SalaryItemType itemType = managementService.findSalaryItemTypeById(saf
				.getTypeId());
		detailType.setType(itemType);
		managementService.saveSalaryItemDetailType(detailType);
		List<SalaryItemDetailType> detailTypes = managementService
				.findSalaryItemDetailType();
		request.setAttribute("detailTypes", detailTypes);
		return mapping.findForward("save");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemDetailTypeActionForm saf = (SalaryItemDetailTypeActionForm) form;

		SalaryItemDetailType detailType = managementService
				.findSalaryItemDetailTypeById(saf.getId());
		detailType.setName(saf.getName());
		SalaryItemType itemType = managementService.findSalaryItemTypeById(saf
				.getTypeId());
		detailType.setType(itemType);
		managementService.updateSalaryItemDetailType(detailType);
		List<SalaryItemDetailType> detailTypes = managementService
				.findSalaryItemDetailType();
		request.setAttribute("detailTypes", detailTypes);

		return mapping.findForward("update");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemDetailTypeActionForm saf = (SalaryItemDetailTypeActionForm) form;
		SalaryItemDetailType detailType = managementService
				.findSalaryItemDetailTypeById(saf.getId());
		List<SalaryItemType> types = managementService.findSalaryItemType();
		request.setAttribute("types", types);
		detailType.setId(detailType.getId());
		detailType.setName(detailType.getName());
		managementService.updateSalaryItemDetailType(detailType);
		request.setAttribute("detailType", detailType);
		return mapping.findForward("toUpdate");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SalaryItemDetailTypeActionForm saf = (SalaryItemDetailTypeActionForm) form;
		managementService.deleteSalaryItemDetailType(saf.getId());
		return mapping.findForward("delete");
	}

}
